from flask import Blueprint, jsonify, request, g
from app.response import ResponseCode, error_json
from app.extension.session.redis_session import r_session
from app.constant import ScopeNumber
from app.model.image_model import ImageModel
from app.extension.database.sqlalchemy import sqla_db
import functools


class V1BluePoint(Blueprint):
    URL_PREFIX_VERSION = '/api/v1'

    def __init__(self,
                 name,
                 import_name,
                 static_folder=None,
                 static_url_path=None,
                 template_folder=None,
                 url_prefix=None,
                 subdomain=None,
                 url_defaults=None,
                 root_path=None):
        super(V1BluePoint, self).__init__(
            name=name,
            import_name=import_name,
            static_folder=static_folder,
            static_url_path=static_url_path,
            template_folder=template_folder,
            url_prefix=self.URL_PREFIX_VERSION+url_prefix,
            subdomain=subdomain,
            url_defaults=url_defaults,
            root_path=root_path
        )


def user_require(view_func):
    @functools.wraps(view_func)
    def inner(*args, **kwargs):
        token = request.headers.get('Token', None)
        if not token:
            return error_json(ResponseCode.FORBIDDEN_REQUEST, errmsg='Token缺失')
        value = r_session.get(token)
        if not value:
            return error_json(ResponseCode.TOKEN_INVALID, errmsg='令牌过期或者无效')
        if value.get('scope', 0) != ScopeNumber.USER:
            return error_json(ResponseCode.PERMISSION_DENIED, errmsg='用户权限不足')
        g.uid = value.get('uid', 0)
        if not g.uid:
            return error_json(ResponseCode.INTERNAL_SERVER_ERROR, errmsg='内部服务异常，请稍后再试')
        return view_func(*args, **kwargs)
    return inner


def admin_require(view_func):
    @functools.wraps(view_func)
    def inner(*args, **kwargs):
        token = request.headers.get('Token', None)
        if not token:
            return error_json(ResponseCode.FORBIDDEN_REQUEST, errmsg='Token缺失')
        value = r_session.get(token)
        if not value:
            return error_json(ResponseCode.TOKEN_INVALID, errmsg='令牌过期或者无效')
        if value.get('scope', 0) != ScopeNumber.ADMIN:
            return error_json(ResponseCode.PERMISSION_DENIED, errmsg='用户权限不足，Need Admin!')
        g.uid = value.get('uid', 0)
        if not g.uid:
            return error_json(ResponseCode.INTERNAL_SERVER_ERROR, errmsg='TOKEN EXCEPTION, 内部服务异常，请稍后再试')
        print('is admin scope:', value.get('scope', 0))
        return view_func(*args, **kwargs)
    return inner


def any_user_require(view_func):
    @functools.wraps(view_func)
    def inner(*args, **kwargs):
        token = request.headers.get('Token', None)
        if not token:
            return error_json(ResponseCode.FORBIDDEN_REQUEST, errmsg='Token缺失')
        value = r_session.get(token)
        if not value:
            return error_json(ResponseCode.TOKEN_INVALID, errmsg='令牌过期或者无效')
        g.uid = value.get('uid', 0)
        if not g.uid:
            return error_json(ResponseCode.INTERNAL_SERVER_ERROR, errmsg='TOKEN EXCEPTION, 内部服务异常，请稍后再试')
        return view_func(*args, **kwargs)
    return inner


